package com.aios.action.user.iris;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.aios.action.AbstractManagementAction;
import com.aios.entity.IrisData;
import com.aios.entity.IrisUser;
import com.aios.entity.mapper.AssignedState;
import com.aios.entity.mapper.IrisUserHasUserGroups;
import com.aios.entity.unit.GenderEnum;
import com.aios.exception.ItemNotFoundException;
import com.aios.exception.NullReferenceAccessException;
import com.aios.service.IrisUserService;

public abstract class AbstractIrisUserAction extends AbstractManagementAction {

	private static final long serialVersionUID = 1L;
	protected IrisUser irisUser;
	protected List<AssignedState> existingGroups;
	protected List<AssignedState> availableGroups;
	protected Map<Integer, String> genderList;
	protected IrisData irisData;
	
	@Qualifier("irisUserService")
	@Autowired
	protected IrisUserService irisUserService;

	public IrisUser findById() {

		if (id != null) {
			try {
				return irisUserService.findById(id);
			} catch (Exception e) {
				throw new ItemNotFoundException("Id : " + id);
			}
		} else {
			// logger
			throw new NullReferenceAccessException("Id cannot be null");
		}
	}

	public IrisUserHasUserGroups findUserWithGroupsByID() {
		if (id != null) {
			try {
				return irisUserService.findByIdWithGroups(id);
			} catch (Exception e) {
				throw new ItemNotFoundException("Id : " + id);
			}
		} else {
			// logger
			throw new NullReferenceAccessException("Id cannot be null");
		}
	}

	public IrisUser getIrisUser() {
		return irisUser;
	}

	public void setIrisUser(IrisUser irisUser) {
		this.irisUser = irisUser;
	}

	public List<AssignedState> getExistingGroups() {
		return existingGroups;
	}

	public void setExistingGroups(List<AssignedState> existingGroups) {
		this.existingGroups = existingGroups;
	}

	public List<AssignedState> getAvailableGroups() {
		return availableGroups;
	}

	public void setAvailableGroups(List<AssignedState> availableGroups) {
		this.availableGroups = availableGroups;
	}

	public void commonPopulate(Locale locale) {
		availableGroups = irisUserService.findAvailableGroups(irisUser.getId());
		genderList = GenderEnum.getMap(locale);

	}

	public void inputPopulate(Locale locale) {
		this.commonPopulate(locale);
	}

	public void editPopulate(Locale locale) {
		this.commonPopulate(locale);
		existingGroups = irisUserService.findAssignedGroups(irisUser.getId());
	}

	public Map<Integer, String> getGenderList() {
		return genderList;
	}

	public void setGenderList(Map<Integer, String> genderList) {
		this.genderList = genderList;
	}

	public IrisData getIrisData() {
		return irisData;
	}

	public void setIrisData(IrisData irisData) {
		this.irisData = irisData;
	}
	
}
